package com.zrf.testchatgpt.test;

//import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import sun.misc.BASE64Decoder;


import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
@Slf4j
public class RuntimeFunction {
    private  CloseableHttpAsyncClient asyncClient = HttpAsyncClients.createDefault();
        public static void main(String[] args) throws Exception {
//            String prompt="nikon RAW photo,8 k,Fujifilm XT3,masterpiece, best quality, 1girl,solo,realistic, photorealistic,ultra detailed,diamond stud earrings, long straight black hair, hazel eyes, serious expression, slender figure, wearing a black blazer and white blouse, standing against a city skyline at night <lora:iu_V35:1>";
//            String negative_prompt="(worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, age spot, glans,extra fingers,fewer fingers,strange fingers,bad hand,";
//            String script_index="DPM++ 2M Karras";
//            String steps="28";
//            String cfg_scale="8";
//            Map<String,Object> map=new HashMap<>();
//            map.put("prompt",prompt);
////            map.put("negative_prompt",negative_prompt);
////            map.put("script_index",script_index);
//            map.put("steps",steps);
//            map.put("cfg_scale",cfg_scale);
//            JSONObject path = doPostTestThree(map);
////            System.out.println("图片路径为:"+path.toString());
//            try {
//
//                //你的endpoint,本机可以不变
//                String endpoint = "http://192.168.137.130:9000";
//                //你的accessKey
//                String accessKey = "LP7PTYS3SL21SIHL3ZV8";
//                //你的sercetKey
//                String secretKey = "GR5X+kMFgjOkBtwjDVNelfwglkBpH1x6cZpRNvuO";
//                //你的bucket
//                String bucketName = "public";
//                // 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
//                MinioClient minioClient = new MinioClient(endpoint, accessKey, secretKey);
//
//                // 检查存储桶是否已经存在
//                boolean isExist = minioClient.bucketExists(bucketName);
//                if (isExist) {
//                    System.out.println("Bucket already exists.");
//                } else {
//                    // 创建一个名为asiatrip的存储桶，用于存储照片的zip文件。
//                    minioClient.makeBucket(bucketName);
//                }
//
//                JSONArray base64=JSONArray.parseArray(path.getString("images"));
//                log.info("图片"+base64.toString()+"图片");
//                InputStream byteArrayInputStream = base64ConvertPNG(base64.toString());
//                    String nameSuffix = UUID.randomUUID().toString() + ".png";
//                    nameSuffix=nameSuffix.replaceAll("-","");
//
//                    // 上传配置
//                    PutObjectOptions options = new PutObjectOptions(byteArrayInputStream.available(), PutObjectOptions.MAX_PART_SIZE);
//                    options.setContentType("image/png");
//                    minioClient.putObject(bucketName, nameSuffix, byteArrayInputStream, options);
//            } catch (MinioException e) {
//                System.out.println("Error occurred: " + e);
////                return null;
//            }
//          new RuntimeFunction().getgpt();
        }


    public static JSONObject doPostTestThree(Map<String,Object> map) {

        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URI uri = null;
        CloseableHttpResponse response = null;
        try {
            // 将参数放入键值对类NameValuePair中,再放入集合中
            List<NameValuePair> params = new ArrayList<>();
            for (Map.Entry<String,Object>  entry:map.entrySet()
                 ) {
                params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
//            new URIBuilder().set
            uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(8000)
                    .setPath("/bat").setParameters(params).build();
            HttpGet httpPost = new HttpGet(uri);

            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

                return  JSON.parseObject(EntityUtils.toString(responseEntity));
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}


